<?php
if(isset($_GET['action'])){
	if ($_GET['action'] == "sendtoserver") { Save(); }
	if ($_GET['action'] == "loadfromserver") { Load(); }
	if ($_GET['action'] == "texttoserver") { SaveText(); }
	if ($_GET['action'] == "loadtextfromserver") { LoadText(); }
}

$cords = array();
function Save() {
	
	$cords = Load();
	
	$dataX = json_decode($_POST['cordX'], true);
	$dataY = json_decode($_POST['cordY'], true);
	$dataDrag = json_decode($_POST['drag'], true);
	$dataId = json_decode($_POST['id'], true);

	for($i = 0; $i < sizeof($dataX); $i++){
		if ($cords[$i][X] == $dataX[$i] && $cords[$i][Y] == $dataY[$i] &&
		    $cords[$i][Id] == $dataId[$i]){
		continue;
		}
		else {
			$cords [] = array(
	  		'X' => $dataX[$i], 
	  		'Y' => $dataY[$i],
			'Drag' => $dataDrag[$i],
			'Id'=> $dataId[$i],
			);
		}
	}

	$doc = new DOMDocument();

	$doc->formatOutput = true;

	$r = $doc->createElement( "cords" );
	$doc->appendChild( $r );

	foreach( $cords as $cord )
	{
		$b = $doc->createElement( "cord" );

		$cordX = $doc->createElement( "X" );
		$cordX->appendChild(
		$doc->createTextNode( $cord['X'] )
		);
		$b->appendChild( $cordX );

		$cordY = $doc->createElement( "Y" );
		$cordY->appendChild(
		$doc->createTextNode( $cord['Y'] )
		);
		$b->appendChild( $cordY );
		
		$drag = $doc->createElement( "Drag" );
		$drag->appendChild(
		$doc->createTextNode( $cord['Drag'] )
		);
		$b->appendChild( $drag );
		
		$Id = $doc->createElement( "Id" );
		$Id->appendChild(
		$doc->createTextNode( $cord['Id'] )
		);
		$b->appendChild( $Id );

		$r->appendChild( $b );
	}

	$doc->saveXML();
	$doc->save("write.xml");

	//$return = array('allerta' => 'a', 'items' => 'b');
	//echo json_encode($return);
	//echo json_encode($cords);
	exit();
}

function Load(){
	$doc = new DOMDocument();

	$doc->load( 'write.xml' );

	$cordsDOM = $doc->getElementsByTagName( "cord" );

	foreach( $cordsDOM as $cord )
	{
		$Xs = $cord->getElementsByTagName( "X" );
		$X = $Xs->item(0)->nodeValue;
			
		$Ys= $cord->getElementsByTagName( "Y" );
		$Y= $Ys->item(0)->nodeValue;
		
		$Drags= $cord->getElementsByTagName( "Drag" );
		$Drag= $Drags->item(0)->nodeValue;
		
		$Ids= $cord->getElementsByTagName( "Id" );
		$Id= $Ids->item(0)->nodeValue;
			
		$cords [] = array(
  			'X' => $X, 
  			'Y' => $Y,
			'Drag' => $Drag,
			'Id' => $Id,  
		);

	}
	echo json_encode($cords);
	return $cords;
}
function SaveText() {
		
	$cords = LoadText();
	
	$textX = json_decode($_POST['textX'], true);
	$textY = json_decode($_POST['textY'], true);
	$textMessage = json_decode($_POST['message'], true);
	$textId = json_decode($_POST['textid'], true);

	for($i = 0; $i < sizeof($textX); $i++){
		if ($cords[$i][X] == $textX[$i] && $cords[$i][Y] == $textY[$i] &&
		    $cords[$i][Id] == $textId[$i]){
			if($cords[$i][Message]!=$textMessage[$i]){
				$cords[$i][Message]=$textMessage[$i];
			}
		}
		else {
			$cords [] = array(
	  		'X' => $textX[$i], 
	  		'Y' => $textY[$i],
			'Message' => $textMessage[$i],
			'Id'=> $textId[$i],
			);
		}
	}

	$doc = new DOMDocument();

	$doc->formatOutput = true;

	$r = $doc->createElement( "texts" );
	$doc->appendChild( $r );

	foreach( $cords as $cord )
	{
		$b = $doc->createElement( "text" );

		$cordX = $doc->createElement( "X" );
		$cordX->appendChild(
		$doc->createTextNode( $cord['X'] )
		);
		$b->appendChild( $cordX );

		$cordY = $doc->createElement( "Y" );
		$cordY->appendChild(
		$doc->createTextNode( $cord['Y'] )
		);
		$b->appendChild( $cordY );
		
		$message = $doc->createElement( "Message" );
		$message->appendChild(
		$doc->createTextNode( $cord['Message'] )
		);
		$b->appendChild( $message );
		
		$Id = $doc->createElement( "Id" );
		$Id->appendChild(
		$doc->createTextNode( $cord['Id'] )
		);
		$b->appendChild( $Id );

		$r->appendChild( $b );
	}

	$doc->saveXML();
	$doc->save("writetext.xml");

	//$return = array('allerta' => 'a', 'items' => 'b');
	//echo json_encode($return);
	//echo json_encode($cords);
	exit();
}
function LoadText() {
	$doc = new DOMDocument();

	$doc->load( 'writetext.xml' );

	$cordsDOM = $doc->getElementsByTagName( "text" );

	foreach( $cordsDOM as $cord )
	{
		$Xs = $cord->getElementsByTagName( "X" );
		$X = $Xs->item(0)->nodeValue;
			
		$Ys= $cord->getElementsByTagName( "Y" );
		$Y= $Ys->item(0)->nodeValue;
		
		$Messages= $cord->getElementsByTagName( "Message" );
		$Message= $Messages->item(0)->nodeValue;
		
		$Ids= $cord->getElementsByTagName( "Id" );
		$Id= $Ids->item(0)->nodeValue;
			
		$cords [] = array(
  			'X' => $X, 
  			'Y' => $Y,
			'Message' => $Message,
			'Id' => $Id,  
		);

	}
	echo json_encode($cords);
	return $cords;
}
?>